<?php
class ApiController extends BaseController{

	public function __construct(){
		parent::__construct();
		$this->beforeFilter('csrf', array('on' => 'post'));
		$this -> beforeFilter('admin');		
	}

	public function getIndex(){
		return View::make('apis.index')
			-> with('title', 'API V3')
			-> with('apis', Api::orderBy('created_at', 'DESC') -> paginate(10));
	}
	
	public function postCreate(){
		$validator = Validator::make(Input::all(),Api::$rules);

		if(!$validator -> fails()){
			$api = new Api;			
			$api -> api_key = Input::get('api_key');	
			$api -> network_id = Input::get('network_id');		
			$api -> save();

			return Redirect::to('admin/apis/index')
					-> with('message', 'Api has been added.');
		}
		return Redirect::to('admin/apis/index')
				-> with('message', 'Whoops, looks like something went wrong.')
				-> withErrors($validator)
				-> withInput();
	}

	public function getDestroy($id){	

		$api = Api::find($id);

		if($api){
			$api -> delete();
			return Redirect::to('admin/apis/index')
				-> with('message','API has been deleted.');
		}
		return Redirect::to('admin/apis/index')
			-> with('message','Whoops, looks like something went wrong..');	
	}

	public function getCategory($id){

		$api = Api::find($id);
		$date = date('Y-m-d', time());

		$key 	 = $api -> api_key;
		$network = $api -> network_id;	

		$URL = 'http://api.hasoffers.com/v3/Affiliate_Offer.json?Method=findMyOffers&api_key='.$key.'&NetworkId='.$network.'';

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$URL);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$output = curl_exec($ch);

		$json_decode = json_decode($output);

		$result = array();
		foreach ($json_decode->{'response'}->{'data'} as $id => $offer) {
			$result[] = array(
				'offer_id'   => $id,
				'offer_name'  => $offer->{'Offer'}->{'name'} 
			);
		}	

		return View::make('apis.category')
			-> with('title', 'Report Summary')
			-> with('key', $key)
			-> with('network', $network)
			-> with('date', $date)
			-> with('result', $result);
	}

	public function postConvertion(){

		$date 	 = date("Y-m-d", strtotime(Input::get('date')));	
		$key 	 = Input::get('key');
		$network = Input::get('network');
		
		$offer = Input::get('offer');

		$URL = 'http://api.hasoffers.com/v3/Affiliate_Report.json?Method=getStats&api_key='.$key.'&NetworkId='.$network.'&limit=0&fields[]=Stat.offer_id&fields[]=Stat.conversions&fields[]=Stat.clicks&fields[]=Stat.payout&filters[Stat.offer_id][conditional]=EQUAL_TO&filters[Stat.offer_id][values][]='.$offer.'&filters[Stat.date][conditional]=BETWEEN&filters[Stat.date][values][]='.$date.'&filters[Stat.date][values][]='.$date.'&groups[]=Stat.offer_id&groups[]=Stat.hour&data_start='.$date.'&data_end='.$date.'&hour_offset=-10&field[]=Stat.conversions&field[]=Stat.clicks&field[]=Stat.payout&timestamp=Stat.hour';


		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$URL);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$output = curl_exec($ch);

		$json_decode = json_decode($output);

		$count = 1; 

		$page 		= $json_decode->{'response'}->{'data'}->{'page'};
		$current 	= $json_decode->{'response'}->{'data'}->{'current'};
		$count 		= $json_decode->{'response'}->{'data'}->{'count'};
		$pageCount 	= $json_decode->{'response'}->{'data'}->{'pageCount'};

		$result = array();
		for ($i = 0; $i < $count; $i++) {
			$result[$i] = array(					
				'hasoffers_offer_id' 			=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'offer_id'},
				'hasoffers_clicks' 				=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'clicks'},
				'hasoffers_conversions' 		=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'conversions'},
				'hasoffers_payout' 				=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'payout'},		
				'hasoffers_hour' 				=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'hour'}		
			);
		}		

		return View::make('apis.view')
			-> with('title', 'Conversion Rate by Hour of Date')			
			-> with('result', $result)
			-> with('count', $count);
	}


	public function postReport(){

		$key 	 = Input::get('key');
		$network = Input::get('network');

		$data_start = date("Y-m-d", strtotime(Input::get('data_start')));
		$data_end   = date("Y-m-d", strtotime(Input::get('data_end')));

		$URL = 'http://api.hasoffers.com/v3/Affiliate_Report.json?Method=getConversions&api_key='.$key.'&NetworkId='.$network.'&page=1&limit=10000&fields[]=Stat.offer_id&fields[]=Country.name&fields[]=Browser.display_name&fields[]=Stat.datetime&fields[]=Offer.name&fields[]=Country.name&fields[]=Browser.display_name&fields[]=Stat.conversion_status&fields[]=Stat.payout&fields[]=Stat.ip&fields[]=ConversionsMobile.device_brand&fields[]=ConversionsMobile.device_model&fields[]=ConversionsMobile.device_os&fields[]=ConversionsMobile.device_os_version&sort[Stat.datetime]=desc&filters[Stat.date][conditional]=BETWEEN&filters[Stat.date][values][]='.$data_start.'&filters[Stat.date][values][]='.$data_end.'&data_start='.$data_start.'&data_end='.$data_end.'&hour_offset=-10&fields[]=Stat.session_ip&fields[]=Stat.ip&fields[]=Stat.user_agent&fields[]=Stat.refer';

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$URL);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$output = curl_exec($ch);


		$json_decode = json_decode($output);

		//$count = 1;

		$page 		= $json_decode->{'response'}->{'data'}->{'page'};
		$current 	= $json_decode->{'response'}->{'data'}->{'current'};
		$count 		= $json_decode->{'response'}->{'data'}->{'count'};
		$pageCount 	= $json_decode->{'response'}->{'data'}->{'pageCount'};

		$result = array();

		if($count > 1){
		for ($i = 0; $i < $count; $i++) {
			$result[$i] = array(		
				'hasoffers_refer' 				=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'refer'},
				'hasoffers_datetime' 			=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'datetime'},
				'hasoffers_offer_id' 			=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'offer_id'},
				'hasoffers_name' 				=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Country'}->{'name'},
				'hasoffers_user_agent' 			=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'user_agent'},
				'hasoffers_browser' 			=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Browser'}->{'display_name'},
				'hasoffers_device_brand' 		=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'ConversionsMobile'}->{'device_brand'},
				'hasoffers_device_model' 		=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'ConversionsMobile'}->{'device_model'},
				'hasoffers_device_os' 			=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'ConversionsMobile'}->{'device_os'},
				'hasoffers_session_ip' 			=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'session_ip'},
				'hasoffers_ip' 					=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'ip'},
				'hasoffers_conversion_status' 	=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'conversion_status'},
				'hasoffers_payout' 				=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'Stat'}->{'payout'},
				'hasoffers_device_os_version'	=> $json_decode->{'response'}->{'data'}->{'data'}[$i]->{'ConversionsMobile'}->{'device_os_version'}
			);
		}
		}
		return View::make('apis.report')
			-> with('title', 'Conversion Report')			
			-> with('result', $result)
			-> with('current', $current)
			-> with('page', $page)
			-> with('count', $count)
			-> with('pageCount', $pageCount);
	}

}